package com.bonc.ioc.util;


import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.InputStreamSource;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * * @description
 *
 * @author wangqinglin@bonc.com.cn
 * @date 2020/12/10 21:28
 * @change: 2020/12/10 21:28 by wangqinglin@bonc.com.cn for init
 */
public class CustomFileUtils {

    /**
     * 上传到本地服务器
     *
     * @param file 输入流
     * @param path 路径 、绝对 、 相对
     * @param name 文件名字
     */
    @SneakyThrows
    public static void uploadSingleFileToPath(InputStreamSource file, String path, String name) {
        InputStream stream = null;
        try {
            stream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileUtils.copyInputStreamToFile(stream, new File(path + File.separator + name));
    }

    /**
     * 下载
     *
     * @param file
     * @param response
     */
    @SneakyThrows
    public static void downloadSingleFileFromPath(File file, HttpServletResponse response) {
        try {

            // 下面是获取文件流并发送给前端，response会将文件流发送给前端
            response.setCharacterEncoding("UTF-8");
            String newName = file.getName();
            String filePrefix = newName.substring(0, newName.lastIndexOf("_"));
            String fileSuffix = newName.substring(newName.lastIndexOf("."), newName.length());
            String simpleName = filePrefix + fileSuffix;
            String newFileName = new String(simpleName.getBytes(), StandardCharsets.UTF_8);
            //response.setContentType("application/octet-stream");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", StrUtil.format("attachment;filename*=utf-8'zh_ch'{}", URLUtil.encode(simpleName, "UTF-8")));
         //   response.setHeader("Content-disposition", "attachment;filename=" + newFileName);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[1024];
            int length;
            while ((length = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
            bis.close();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//        // 设置强制下载不打开
//        response.reset();
//        // 设置文件名
//        String newName = file.getName();
//        String filePrefix = newName.substring(0, newName.lastIndexOf("_"));
//        String fileSuffix = newName.substring(newName.lastIndexOf("."), newName.length());
////        response.setContentType("application/octet-stream");
////        response.setCharacterEncoding("UTF-8");
//        String name = filePrefix + fileSuffix;
////        response.setContentType("multipart/form-data");
//        // response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + new String(filePrefix.getBytes()) + fileSuffix);
//        byte[] buffer = new byte[1024];
//        FileInputStream fis = null;
//        try {
//            fis = new FileInputStream(file);
//            OutputStream os = response.getOutputStream();
//            int i;
//            while ((i = fis.read(buffer)) != -1) {
//                os.write(buffer, 0, i);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (fis != null) {
//                try {
//                    fis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//}


}
